//
//  FileUtil.swift
//  Game
//
//  Created by woniu on 2025/8/27.
//

import Foundation
import UIKit

class FileUtil:NSObject{
    
    public static let share = FileUtil()
    
    override init() {
        super.init()
        //创建存放图片的文件夹
        guard let path = getImagesDirectory()else{return}
        if !FileManager.default.fileExists(atPath:path){
           try? FileManager.default.createDirectory(atPath: path, withIntermediateDirectories: true)
        }
        //创建配置文件
        guard let configPath = getConfigPath()else{
            return
        }
        if !FileManager.default.fileExists(atPath:configPath){
           FileManager.default.createFile(atPath: configPath, contents: nil)
        }
    }
    
    
    private func getImagesDirectory()->String?{
        if let path = NSSearchPathForDirectoriesInDomains(.documentDirectory,.allDomainsMask,true).last{
            return path + "/GameImages"
        }
        print("获取路径为空了-----getImagesDirectory")
        return nil
    }
    
    private func getConfigPath()->String?{
        if let path = NSSearchPathForDirectoriesInDomains(.documentDirectory,.allDomainsMask,true).last{
            return path + "/gameConfig.json"
        }
        print("获取路径为空了-----getConfigPath")
        return nil
    }
    
    //删除图片
     func delete(imgNames:[String]){
         guard let imgPath = getImagesDirectory()else{return}
        for imgName in imgNames {
            if !imgName.isEmpty{
                let imgPath = imgPath + "/\(imgName).png"
                if FileManager.default.fileExists(atPath:imgPath){
                    let url = URL(fileURLWithPath: imgPath)
                    try? FileManager.default.removeItem(at: url)
                }
            }
        }
    }
    
    //获取图片
    public func getImageBy(_ name:String)->UIImage?{
        if name.isEmpty{return nil}
        guard let directory = getImagesDirectory()else{return nil}
        let imgPath = directory + "/\(name).png"
        let isExists = FileManager.default.fileExists(atPath:imgPath)
        if !isExists{
            return nil
        }
        return UIImage(contentsOfFile: imgPath)
    }
    
    //保存图片
     func saveOrReplace(name:String,image:Data){
        if name.isEmpty{return}
         guard let directory = getImagesDirectory()else{return}
         let imgPath = "\(directory)/\(name).png"
        let isExists = FileManager.default.fileExists(atPath:imgPath)
        //先删除
        if isExists{
           delete(imgNames: [name])
        }
         //替换
        FileManager.default.createFile(atPath: imgPath, contents: image)
    }
    
    //获取配置
    public func getGameConfigList()->[[String:Any]]{
        guard  let path = getConfigPath()else{return []}
        if let content = try? String(contentsOfFile:path){
            let list = content.getArrayFromJSONString()
            if let contentList = list as? [[String:Any]]{
                return contentList
            }
        }
        return []
    }
    
    //写入文件
    public func writeConfig(_ json:String){
        guard let path = getConfigPath()else{return}
        let isExists = FileManager.default.fileExists(atPath:path)
        
        if !isExists,let data = json.data(using: .utf8){
            FileManager.default.createFile(atPath: path, contents: data)
        }else{
            try? json.write(toFile: path, atomically: true, encoding: .utf8)
        }
    }
}
